import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;


public class AnimImageTileGenerator extends JFrame {
	BufferedImage splat, drops, selector;


	int framecounter = 0;

	public AnimImageTileGenerator() throws IOException{
		//from this
		int rp = 255, gp=255, bp=0;
		//to this
		int rl=0, gl=0, bl = 0;
		//and back to the first one.
		int rd = (rp>rl)? 1:-1, gd = (gp>gl)? 1:-1, bd =(bp>bl)? 1:-1; 
		
		
		
		
		int numFrames = 60*3;
		int framewidth = 6, frameheight=23;
		
		
		BufferedImage bfinal = new BufferedImage(numFrames * framewidth, frameheight,BufferedImage.TYPE_INT_ARGB);



		for(int t = 0; t <numFrames ;t++){
			selector = new BufferedImage(framewidth, frameheight,BufferedImage.TYPE_INT_ARGB);
			
			
			Graphics2D g = (Graphics2D)selector.getGraphics();
			//high quality shit
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
			
			
			Color interp = new Color(
			255-(int)Math.round(rl +  (rp - rl) * .5 * ( 1+ rd* Math.cos( 2* Math.PI* t/ (double)numFrames))),

			(int)Math.round(gl +  (gp - gl) * .5 * ( 1+ gd* Math.cos( 2* Math.PI* t/ (double)numFrames))),
			
			(int)Math.round(bl +  (bp - bl) * .5 * ( 1+ bd* Math.cos( 2* Math.PI* t/ (double)numFrames)))   /**/   );
			
			System.out.println(interp.toString());
			
			
			
			g.setColor(interp);
			
//			g.setColor(Color.pink);
			
//			g.fill(new Rectangle( 28,0+8 ,32 ,32-8 -8));
			g.fill(new Rectangle(0,0,framewidth,frameheight));
			
			
			bfinal.getGraphics().drawImage(selector, t*framewidth, 0, null);
//			System.out.println("wrote out sprite "+t);
		}

		ImageIO.write(bfinal, "png", new File("selector_sprite.png"));
		System.out.println("done");
		
	}


	public void printPixelARGB(int pixel) {
		int alpha = (pixel >> 24) & 0xff;
		int red = (pixel >> 16) & 0xff;
		int green = (pixel >> 8) & 0xff;
		int blue = (pixel) & 0xff;
		System.out.println("argb: " + alpha + ", " + red + ", " + green + ", " + blue);
	}

	public void generateSplatSprite() throws IOException{
		BufferedImage bfinal = new BufferedImage(300 * 80, 100,BufferedImage.TYPE_INT_ARGB);


		this.setSize(600,600);
		drops = ImageIO.read(new File("splat_trail.png"));

		for(int t = 0; t <300;t++){
			splat = new BufferedImage(80,94,BufferedImage.TYPE_INT_ARGB);

			//Draw droplets in correct position based on time elapsed
			if( t<10){
				//do nothing
			}
			else if( t>=30){
				splat.getGraphics().drawImage(drops, +12, (int)(1.2*Math.pow(t-30, .5)), null);
				splat.getGraphics().drawImage(drops, -4, (int)(2*Math.pow(t-30, .5)), null);
			}

			//draw the general splat over-top
			splat.getGraphics().drawImage( ImageIO.read(new File("splat_base.png")),0,0,null);

			//draw the splat frame on the large tiled-image
			bfinal.getGraphics().drawImage(splat, t*80, 0, null);

			//finished!
			System.out.println("wrote out splat."+t+".png");
		}

		ImageIO.write(bfinal, "png", new File("final.png"));
		System.out.println("done");
	}

	public static void main(String[]args) throws IOException{
		new AnimImageTileGenerator();
	}


}

